﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.BackdropForm;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using System.Linq;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.WP.RecordCenter.BackDropForm
{
    public partial class BackDropFormUserControl : BaseControl, IBackdropFormView
    {
        #region [Private Fields]

        BackdropFormPresenter _Presenter;

        #endregion

        #region [Event Handlers]

        protected void btnSubmitOperation_Click(object sender, EventArgs e)
        {
            try
            {
                if (ItemID.HasValue)
                {
                    _Presenter.Update();
                    HandleSuccess("El fondo ha sido actualizado exitosamente", new WebControl[] { btnSave });
                }
                else
                {
                    _Presenter.Add();
                    HandleSuccess("El fondo ha sido agregado exitosamente", new WebControl[] { btnSave });
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnCancelOperation_Click(object sender, EventArgs e)
        {
            string isDlg = Request.QueryString.Get("IsDlg");
            if (!string.IsNullOrEmpty(isDlg) && isDlg.Equals("1")) { this.CloseWindow(); }
            else { this.GoCurrentUserHome(); }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            _Presenter = new BackdropFormPresenter(this);
            txtbTitle.Required("Este campo es requerido", btnSave.ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            txtbCode.Required("Este campo es requerido", btnSave.ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            txtbVersion.Required("Este campo es requerido", btnSave.ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            txtbVersion.Numeric("9999", false);
            btnSave.AddLoader();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack) { _Presenter.HandleLoad(); }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region [IBackdropFormView Members]

        public string Title
        {
            get
            {
                return txtbTitle.Text;
            }
            set
            {
                txtbTitle.Text = value;
            }
        }

        public string Code
        {
            get
            {
                return txtbCode.Text;
            }
            set
            {
                txtbCode.Text = value;
            }
        }

        public double? Version
        {
            get
            {
                return string.IsNullOrEmpty(txtbVersion.Text) ? new Nullable<Double>() : Convert.ToDouble(txtbVersion.Text);
            }
            set
            {
                txtbVersion.Text = value.HasValue ? value.Value.ToString() : string.Empty;
            }
        }

        public int? ItemID
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString.Get("ID")) ? new Nullable<Int32>() : Convert.ToInt32(Request.QueryString.Get("ID"));
            }
        }

        public DAL.RecordCenter.FondosFondo SelectedParent
        {
            get
            {
                if (ddlBackdrops.SelectedValue.Equals("0")) { return null; }

                else
                {
                    int selectedId = Convert.ToInt32(ddlBackdrops.SelectedValue);
                    return Backdrops.SingleOrDefault(backdrop => backdrop.Id.Equals(selectedId));
                }
            }
            set
            {
                ddlBackdrops.ClearSelection();
                if (value == null) { ddlBackdrops.SelectedIndex = 0; }
                else { ddlBackdrops.SelectedValue = value.Id.ToString(); }
            }
        }

        public System.Collections.Generic.IEnumerable<DAL.RecordCenter.FondosFondo> Backdrops
        {
            get
            {
                return Cache[string.Format("BackdropForm_Backdrops_{0}", SPContext.Current.Web.CurrentUser.LoginName)] as System.Collections.Generic.IEnumerable<DAL.RecordCenter.FondosFondo>;
            }
            set
            {
                Cache[string.Format("BackdropForm_Backdrops_{0}", SPContext.Current.Web.CurrentUser.LoginName)] = value;
                ddlBackdrops.DataSource = value;
                ddlBackdrops.DataBind();
                ddlBackdrops.Items.Insert(0, new ListItem { Text = GlobalConstants.DDL_INITIAL_STATE, Value = "0" });
            }
        }

        #endregion
    }
}
